﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Windows.Controls;
using System.Windows.Data;

namespace DataAchive.Helper
{
    public class DataGridHelper
    {
        public void DataGridBuilder(Type type, DataGrid viewData, List<string> checkedColumns = null)
        {
            var properties = type.GetProperties();
            // 反转顺序，使基类的属性在前
            properties.Reverse();
            // 根据属性动态添加列
            foreach (var property in properties)
            {

                DescriptionAttribute descriptionAttr = (DescriptionAttribute)property.GetCustomAttribute<DescriptionAttribute>();
                string description = descriptionAttr == null ? property.Name : descriptionAttr.Description;
                if (checkedColumns == null || checkedColumns.Contains(description))
                {
                    // 创建 DataGridTextColumn
                    var column = new DataGridTextColumn();
                    column.Header = description; // 列标题为属性名
                    column.Binding = new Binding(property.Name); // 绑定到对应属性
                    column.Width = new DataGridLength(1, DataGridLengthUnitType.Star);
                    viewData.Columns.Add(column);
                }

            }
        }

    }
}
